package com.guochenglang.www.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 密码加密
 *
 * @author GuoChenglang
 */
public class Encryption {
    /**
     * 加密类型
     */
    private static final String SHA_256_ALGORITHM = "SHA-256";

    /**
     * SHA-256加密算法
     */
    public static String encryption(String password) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance(SHA_256_ALGORITHM);
        byte[] digest = messageDigest.digest(password.getBytes());
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : digest) {
            stringBuilder.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 3);
        }
        return stringBuilder.toString();
    }
}
